<%@page contentType="application/json;charset=UTF-8"
 %><%@page import="be.destin.skos.core.SkosManager"
 %><%@page import="be.destin.skos.html.SkosHtmlManager"
 %><%@page import="be.destin.skos.core.SchemeInterface"
 %><%@page import="be.destin.skos.core.SchemeUsage"
 %><%@page import="be.destin.skos.core.ConceptScheme"
 %><%@page import="be.destin.skos.core.Concept" 
 %><%@page import="be.destin.skos.core.Term" 
 %><%@page import="be.destin.skos.core.Profile" 
 %><%@page import="be.destin.skos.core.ReferringApplication"
 %><%@page import="be.destin.skos.search.SkosSearch" 
 %><%@page import="be.destin.skos.search.LabelMatchType" 
 %><%@page import="be.destin.skos.search.SearchResult" 
 %><%@page import="be.destin.skos.view.SkosView" 
 %><%@page import="be.destin.skos.view.SkosViewType" 
 %><%@page import="be.destin.skos.view.SkosViewManager" 
 %><%@page import="be.destin.util.Util" 
 %><%@page import="java.util.List" 
 %><%@page import="java.util.Map" 
 %><%@page import="java.util.HashSet" 
 %><%@page import="java.util.ArrayList" 
 %><%@page import="java.util.TreeMap" 
 %><%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" 
 
 %><%	 
   SkosHtmlManager skosHtmlManager = SkosHtmlManager.getAskosiSessionInstance(request);
   SkosManager skosManager = skosHtmlManager.getSkosManager();
   String language = skosHtmlManager.getLanguage();
   Profile aProfile = skosHtmlManager.getProfileData();
   String[] schemeCodes = request.getParameterValues("scheme");
   if (schemeCodes == null || schemeCodes.length == 0) schemeCodes = request.getParameterValues("list");
   if (schemeCodes != null && schemeCodes.length == 1) {
	   schemeCodes = schemeCodes[0].split("\\,"); 
   }
   String sort = request.getParameter("sort");
   String query = request.getParameter("query");
   if (query==null || query.isEmpty()) query = request.getParameter("q");
   if (query==null || query.isEmpty()) query = request.getParameter("current");
   if (query==null || query.isEmpty()) query = request.getParameter("term");
   String basket = request.getParameter("basket");
   String focus = request.getParameter("focus");
   String appFocus = null;
   String roleFocus = null;
   if (focus != null && !focus.isEmpty()) {
	   int pSep = focus.indexOf(SchemeInterface.separator);
	   if (pSep >= 0) {
		   appFocus = focus.substring(0,pSep);
		   roleFocus = focus.substring(pSep+1);
	   }
   }
   String strictlanguage = request.getParameter("strict");
   boolean strictLanguage = strictlanguage != null && strictlanguage.equalsIgnoreCase("on");
   String leftTruncation = request.getParameter("leftTruncation");
   boolean truncateLeft = "on".equalsIgnoreCase(leftTruncation);
   if (!truncateLeft) {
	   if (!query.isEmpty()) {
		   if (query.charAt(0)==' ') {
			   truncateLeft = true;
			   query = query.substring(1);
		   }
	   }
   }
   String byPieces = request.getParameter("byPieces");

   ConceptScheme aScheme = null;
   int nbColumns = 0;
   ArrayList<SchemeInterface> schemeList = new ArrayList<SchemeInterface>();
   if (schemeCodes != null) {
	   for (String schemeCode : schemeCodes) {
		   if (schemeCode.isEmpty()) continue;
		   if ("on".equalsIgnoreCase(schemeCode)) continue;
	       aScheme = (ConceptScheme)skosManager.getScheme(schemeCode);
	       if (aScheme != null) {
	    	   schemeList.add(aScheme);
	       }
	   }
   } else if (aProfile != null) {
	   List<String>browsed = aProfile.getBrowsedList();
	   for (String schemeCode : browsed) {
		   if (schemeCode.isEmpty()) continue;
	       aScheme = (ConceptScheme)skosManager.getScheme(schemeCode);
	       if (aScheme != null) {
	    	   schemeList.add(aScheme);
	       }
	   }
   }
   long searchTime = System.currentTimeMillis();
   ArrayList<SearchResult> result = null;
   if (query != null) {
		LabelMatchType matchType = LabelMatchType.STARTS_WITH;
		if ( "on".equals(leftTruncation)) {
			matchType = LabelMatchType.ANYWHERE;
		}
		if ("on".equals(byPieces)) {
			matchType = LabelMatchType.INDEPENDANT_PIECES;
		}
		result = SkosSearch.searchWithResults(skosHtmlManager, strictLanguage, schemeList.toArray(new SchemeInterface[0]),query, matchType ,null);
   } else if (basket != null) {
		query = "Your Basket!";
		HashSet<Concept> unsorted = skosManager.toSetScheme_Concept(basket,aScheme, null,null,true);
		TreeMap<String,SearchResult> sorter = new TreeMap<String,SearchResult>();
		for (Concept aConcept: unsorted) {
			String key = aConcept.getInScheme().getAbout()+' '+aConcept.getSortLabel(language);
			if (key != null) {
				sorter.put(key,new SearchResult(aConcept.getPrefLabel(language, strictLanguage), "", language, "", aConcept.getScheme_About(), 0.0f));
			}
		}
		result = new ArrayList<SearchResult>(sorter.values());
   }
   if (result == null) {
		result = new ArrayList<SearchResult>();
   }
   searchTime = System.currentTimeMillis() - searchTime;
   int resultSize = 0;
   if (result != null && !result.isEmpty()) {
		resultSize = result.size();
   }
%><%--<%=query%> <%if(schemeCode != null) {%>within Scheme <%=schemeCode%><%}%>: <%=result.size()%>  <small>CPU time: <%=searchTime%>ms
   if(schemeCode == null || "".equals(schemeCode)) {
     if (resultSize > 0) {
		 int compte = 0;
		 ConceptScheme precScheme = null;
	     for(Concept aConcept: result) {
			if (aConcept.getInScheme() != precScheme) {
				if (compte > 0) {
					%>href="#result_<%=precScheme.getAbout()%>"/><%=skosHtmlManager.toHtmlImage(precScheme.getIconAdapted(skosHtmlManager),precScheme.getAbout(),false)%><%=precScheme.getTitle(language)%>:<big><%=compte%>
				}
				precScheme = aConcept.getInScheme();
			  compte = 0;
			}
			compte++;
		 }
		 if (compte > 0) {
			%>href="#result_<%=precScheme.getAbout()%>"/><%=skosHtmlManager.toHtmlImage(precScheme.getIconAdapted(skosHtmlManager),precScheme.getAbout(),false)%><%=precScheme.getTitle(language)%>:<big><%=compte%>
		 }
	 }
   }
--%>[<%
   if (resultSize > 0) {
		 int compte = 0;
		 ConceptScheme precScheme = null;
		 char initiale = 0x00;
		 //String[] QUERIES = Util.prepareHighlight(query,"on".equals(byPieces));
	     for(SearchResult sr : result) {
	    	Concept aConcept = skosManager.getScheme_Concept(sr.getAbout());
	    	int size = 0;
			if (appFocus != null && roleFocus != null) {
				ReferringApplication ra = aConcept.getUsage().getReferringApplication(appFocus);
				if (ra != null) {
					size = ra.getCountCount(roleFocus);
					if (size <= 0) continue;  // Do not show UNUSED concepts if a focus is specified
				} else continue;
			}
	    	Term theLabel = aConcept.getPrefLabelTerm(language,false);
		    char currLetter = Util.noAccent((""+theLabel.getIndexLABEL().charAt(0)).toUpperCase()).charAt(0);
			if (aConcept.getInScheme() != precScheme || currLetter != initiale) {
			  if (aConcept.getInScheme() != precScheme) {
				  precScheme = aConcept.getInScheme();
			  }
			  initiale = currLetter;
			}
			if (compte > 0) {%>, <%}
			compte++;
		    String aName = theLabel.getLabel();
			if (aName == null || aName.length() <= 0) {
				aName = aConcept.getAbout();
			} else {
				aName = Util.escapeJSON(aName);
			}
			String conceptIcon = Util.escapeJSON(aConcept.getIconAdapted(skosHtmlManager));
			String schemeIcon = Util.escapeJSON(aConcept.getInScheme().getIconAdapted(skosHtmlManager));

			String scopeNote = Util.escapeJSON(aConcept.getScopeNote(language));

			//if (QUERIES != null) display = Util.highlight(display,QUERIES);
			%>{ <%if (schemeIcon != null && !schemeIcon.isEmpty()){
				%>"icon":"<%=schemeIcon %>",<%} 
			if (conceptIcon != null && !conceptIcon.isEmpty()){ 
				%> "thumbnail":"<%=conceptIcon %>",<%
			}
			if (scopeNote != null && !scopeNote.isEmpty()){ 
				%><% // "desc":"<--%=scopeNote %-->",
			}
			if (size > 0) {
				%> "size":"<%=size%>",<%
			}
			%> "value":"<%=aConcept.getScheme_About() %>","label":"<%=aName %>","lang":"<%=theLabel.getLang()%>"<%
			String match = sr.getSynonym();
			if (match != null && !match.isEmpty()) {
				%>,"match":"<%=sr.getSynonym()%>","matchlang":"<%=sr.getSynonymLanguage()%>"<%
			}
			%>}<%
		 }
    }%>]
